<%@page import="sun.net.www.content.text.plain"%>
<%@page import="com.mconnect.msurvey.model.SummaryCost"%>
<%@page import="com.mconnect.msurvey.model.SurveyPlan"%>
<%@page import="java.text.DecimalFormat"%>
<%@page import="java.text.DecimalFormatSymbols"%>
<%@page import="com.mconnect.msurvey.model.ReportCost"%>
<%@page import="com.mconnect.msurvey.controller.UserManager"%>
<%@page import="com.mconnect.msurvey.model.User"%>
<%@page import="com.mconnect.msurvey.controller.PlanManager"%>
<%@page import="com.mconnect.msurvey.model.Report"%>
<%@page import="com.mconnect.msurvey.model.PriceListRecommend"%>
<%@page import="com.mconnect.msurvey.model.PriceListSurvey"%>
<%@page import="java.util.ArrayList"%>
<%@page import="com.mconnect.msurvey.model.PriceList"%>
<%@page import="java.util.List"%>
<%@page import="com.mconnect.msurvey.controller.PriceListManager"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<style>
body {
	font-size: 62.5%;
}

label,input {
	display: block;
}

input.text {
	margin-bottom: 12px;
	width: 95%;
	padding: .4em;
}

fieldset {
	padding: 0;
	border: 0;
	margin-top: 25px;
}

h1 {
	font-size: 1.2em;
	margin: .6em 0;
}

div#users-contain {
	margin: 20px 0;
}

div#users-contain table {
	margin: 1em 0;
	border-collapse: collapse;
}

div#users-contain table td,div#users-contain table th {
	border: 1px solid #eee;
	padding: .6em 10px;
	text-align: left;
}

b {
	color: red;
}
</style>
</head>
<body>

	<%
		List<User> userList = new ArrayList<User>();
		UserManager uM = new UserManager();
		int id_plan = 0;
		String dateFrom = request.getParameter("dateFrom");
		String dateTo = request.getParameter("dateTo");
		userList = uM.getAllUsersFromDB();
		PlanManager pM = new PlanManager();
		DecimalFormatSymbols symbols = DecimalFormatSymbols.getInstance();
        symbols.setDecimalSeparator('.');
        DecimalFormat df = new DecimalFormat("0.00", symbols);
	%>
	<div id="users-contain" class="ui-widget" style="width: 900px" >
		<div style="width: 900px">
		
		<table id="campaigns" class="ui-widget ui-widget-content" style="display: inline; margin-left: 25px; margin-top: 20px; width: 880px;">
				<thead>
					<tr style="background-color: #1b3a4e; color: white;">
						<th colspan="10" style="text-align: center;">Zestawienie Kampanii</th>
					</tr>
					<tr class="ui-widget-header ">
						<td style="width: 150px;">Kampania:</td>
						<th style="width: 81px;">Ankiet</th>
						<th style="width: 81px;">Ankiet HR</th>
						<th style="width: 81px;">Wszystkich ankiet</th>
						<th style="width: 81px;">Poleceń</th>
						<th style="width: 81px;">Poleceń HR</th>
						<th style="width: 81px;">Wszystkich poleceń</th>
						<th style="width: 81px;">Potwierdziło przybycie</th>
						<th style="width: 81px;">Pozyskanych peseli</th>
						<td style="width: 81px;">Koszt</td>
					</tr>
				</thead>
				<tbody>
				<% List<SurveyPlan> planList = pM.getPlanListByDate(dateFrom, dateTo);
					double sumOfCampaign = 0.0;
					int sumOfSurveys = 0, sumOfSurveysHr = 0, sumOfTotalSurveys =0, sumOfConfirms =0, sumOfRecommend = 0, sumOfRecommendHR = 0, sumOfTotalRecommend = 0, sumOfPesels = 0;
					if(!planList.isEmpty()){
					for(SurveyPlan plan : planList){ 
						SummaryCost summary = pM.getSummaryReportByIdPlan(plan.getId_plan(), dateFrom, dateTo, plan.getId_price().getResponderPriceList(), plan.getId_price().getRecommendPriceList());
						sumOfCampaign += summary.getReportCost().getTotalCost();
						int numberOfSurveys = summary.getReport().getNumberOfSurveys();
						int numberOfSurveysHR = summary.getReport().getNumberOfSurveysHR();
						int totalSurveys = summary.getReport().getTotalSurveys();
						int numberOfRecommend = summary.getReport().getNumberOfRecommend();
						int numberOfRecommendHR = summary.getReport().getNumberOfRecommendHr();
						int totalRecommend = summary.getReport().getTotalRecommend();
						int numberOfConfirms = summary.getReport().getNumberOfConfirms();
						int numberOfPesels = summary.getReport().getNumberOfPesels();
						sumOfSurveys += numberOfSurveys;
			    		sumOfSurveysHr += numberOfSurveysHR;
			    		sumOfTotalSurveys += totalSurveys;
			    		sumOfConfirms += numberOfConfirms;
			    		sumOfRecommend += numberOfRecommend;
			    		sumOfRecommendHR += numberOfRecommendHR;
			    		sumOfTotalRecommend += totalRecommend;
			    		sumOfPesels += numberOfPesels;
					%>
					
					<tr>
						<td style="color: #1b3a4e"> <%= plan.getPlanName() %></td>
						<td><%= numberOfSurveys %></td>
						<td><%= numberOfSurveysHR %></td>
						<td><%= totalSurveys %></td>
						<td><%= numberOfRecommend %></td>
						<td><%= numberOfRecommendHR %></td>
						<td><%= totalRecommend %></td>
						<td><%= numberOfConfirms %></td>
						<td><%= numberOfPesels %></td>
						<td><b><%= df.format(summary.getReportCost().getTotalCost()) %>zł</b></td>
					</tr>
				<% } %>	
				<tr style="background-color: #d4d4d4; font-weight: bold;">
					<td>Suma</td>
					<td><%= sumOfSurveys %></td>
					<td><%= sumOfSurveysHr %></td>
					<td><%= sumOfTotalSurveys %></td>
					<td><%= sumOfRecommend %></td>
					<td><%= sumOfRecommendHR %></td>
					<td><%= sumOfTotalRecommend %></td>
					<td><%= sumOfConfirms %></td>
					<td><%= sumOfPesels %></td>
					<td><b><%= df.format(sumOfCampaign) %>zł</b></td>
				</tr>
				<%}else{ %>
					<tr>
						<td colspan="10">Brak wyników</td>
					</tr>
				<%} %>	
				</tbody>
			</table>
		
			<div style="display: inline; margin-top: 20px; width: 450px;">
				<table id="users" class="ui-widget ui-widget-content" style="width: 400px; margin-left: -450px; ">
				<thead>
					<tr style="background-color: #1b3a4e; color: white;">
						<th colspan="4" style="text-align: center;">Zestawienie ankieterów</th>
					</tr>
				</thead>
				<%
					for (User user : userList) {
				%>
				<tbody>
					<tr style="font-weight: bold;">
						<td>Ankieter:</td>
						<td colspan="3" style="color: #1b3a4e"><%=user.getFirstname() + " " + user.getLastname()%></td>
					</tr>
					<tr class="ui-widget-header ">
						<td style="width: 160px;">Kampania</td>
						<td style="width: 80px;">Ankiet</td>
						<td style="width: 80px;">Poleceń</td>
						<td style="width: 80px;">Koszt</td>
					</tr>
					<%
						List<SummaryCost> summaryCosts = pM.getSummaryReportByIdUser(user.getId_user(), dateFrom, dateTo);
						double sumOfCosts = 0;
							if (summaryCosts.isEmpty()) {
					%>
					<tr>
						<td colspan="4">Brak wyników</td>

					</tr>
					<%
						}
							for (SummaryCost summary : summaryCosts) {
								sumOfCosts += summary.getReportCost().getTotalCost();
					%>
					<tr>
						<td><%=summary.getPlanName()%></td>
						<td><%=summary.getReport().getNumberOfSurveys()%></td>
						<td><%=summary.getReport().getNumberOfRecommend()%></td>
						<td><b><%= df.format(summary.getReportCost().getTotalCost()) %>zł</b></td>
					</tr>
					
						<%	} %>
					<tr>
						<td  colspan="3">Suma</td>
						<td><b><%= df.format(sumOfCosts) %>zł</b></td>
					</tr>
					<tr>
						<td colspan="4">&nbsp</td>
					</tr>
					<% } %>
				</tbody>
			</table>
			</div>
			
		</div>

	</div>
</body>
</html>